package com.ctsi.commons.util;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;

public  final class UtilURL {

	public static final String PropertyName = ".properties";

	public static URL fromClass(Class<?> contextClass) {
		String resourceName = contextClass.getName();
		int dotIndex = resourceName.lastIndexOf('.');

		if (dotIndex != -1)
			resourceName = resourceName.substring(0, dotIndex);
		resourceName += PropertyName;

		return fromResource(contextClass, resourceName);
	}

	public static URL fromResource(String resourceName) {
		return fromResource(resourceName, null);
	}

	public static URL fromResource(Class<?> contextClass, String resourceName) {
		if (contextClass == null)
			return fromResource(resourceName, null);
		else
			return fromResource(resourceName, contextClass.getClassLoader());
	}

	public static URL fromResource(String resourceName, ClassLoader loader) {
		URL url = null;
		boolean isper = resourceName.endsWith(PropertyName);
		if (loader != null)
			url = loader.getResource(resourceName);
		if (loader != null && url == null && !isper)
			url = loader.getResource(resourceName + PropertyName);

		if (loader == null && url == null) {
			try {
				loader = Thread.currentThread().getContextClassLoader();
			} catch (SecurityException e) {
			 
				loader = UtilURL.class.getClassLoader();
			}
			if (loader == null) {
				loader = UtilURL.class.getClassLoader();
			}
			if (loader == null) {
				throw new RuntimeException("loader is null");
			}
		}

		if (url == null)
			url = loader.getResource(resourceName);
		if (url == null && !isper)
			url = loader.getResource(resourceName + PropertyName);

		if (url == null)
			url = fromUrlString(resourceName);

		if (url == null)
			url = ClassLoader.getSystemResource(resourceName);
		if (url == null && !isper)
			url = ClassLoader.getSystemResource(resourceName + PropertyName);

		if (url == null)
			url = fromFilename(resourceName);
		return url;
	}

	public static URL fromFilename(String filename) {
		if (filename == null || filename.length() == 0)
			return null;
		File file = new File(filename);
		URL url = null;

		try {
			if (file.exists())
				url = file.toURI().toURL();
		} catch (java.net.MalformedURLException e) {
			e.printStackTrace();
			url = null;
		}
		return url;
	}

	public static URL fromUrlString(String urlString) {
		URL url = null;
		try {
			if (urlString.indexOf(':') > -1)
				url = new URL(urlString);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}

		return url;
	}

	public static String readUrlText(URL url) throws IOException {
		InputStream stream = url.openStream();

		StringBuilder buf = new StringBuilder();
		BufferedReader in = null;
		try {
			in = new BufferedReader(new InputStreamReader(stream));

			String str;
			while ((str = in.readLine()) != null) {
				buf.append(str);
				buf.append(System.getProperty("line.separator"));
			}
		} catch (IOException e) {

			throw e;
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return buf.toString();
	}

}
